package cn.nupt.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.nupt.model.Member;
import cn.nupt.util.DBTool;

public class MemberDAOImpl implements MemberDAO{
	
	@Override
	//插入会员操作，返回受影响的行数
	public int insertMember(Member member) {
		int row = 0;//受影响的行数
		Connection conn = null;
		PreparedStatement prst = null;
		String sql = "insert into member (name,phone,address,job) values(?,?,?,?)";
		conn = DBTool.getConnection();
		try{
			prst = conn.prepareStatement(sql);
			prst.setString(1, member.getName());
			prst.setString(2, member.getPhone());
			prst.setString(3, member.getAddress());
			prst.setString(4, member.getJob());
			row = prst.executeUpdate();
		}catch(SQLException ex ){
			ex.printStackTrace();
		}finally{
			DBTool.close(prst, conn);	//	注意关闭数据库相关连接
		}
		
		return row;
	}

	@Override
	//根据会员电话号码，返回会员的信息
	public Member queryMemByPhone(String phone) {
		Connection conn = null;
		PreparedStatement prst = null;
		ResultSet rs = null;
		Member member = null;
		String sql = "select * from member where phone=?";
		conn = DBTool.getConnection();
		try{
			prst = conn.prepareStatement(sql);
			prst.setString(1, phone);
			rs = prst.executeQuery();
			if(rs.next()){
				member = new Member();
				member.setMemberId(rs.getInt("member_id"));
				member.setName(rs.getString("name"));
				member.setPhone(rs.getString("phone"));
				member.setAddress(rs.getString("address"));
				member.setJob(rs.getString("job"));
			}
		}catch(SQLException ex ){
			ex.printStackTrace();
		}finally{
			DBTool.close(prst, conn);	//	注意关闭数据库相关连接
		}
		
		return member;
	}

}
